local function getupvaluetable(func, inname)
    local i = 1
    while true do
        local name, value = debug.getupvalue(func, i)
        if name == nil then
            return
        end
        
        if name == inname then
            return i,value
        end
        
        i=i+1
    end
end


local lua = _P.lua
local idx, old_full_coins_for_robot = getupvaluetable(lua.REQUESTS.get_robot,'full_coins_for_robot')
local _, reason = getupvaluetable(old_full_coins_for_robot,'reason')
local _, math_ramdon = getupvaluetable(old_full_coins_for_robot,'math_ramdon')

local function full_coins_for_robot(robot_info,coin_conf)
    local uid = robot_info.uid

    --判断机器人的类型，为其增加相应的金币
    local type = robot_info.type
    
    local coins = coin_conf.max
    if coin_conf.min < coin_conf.max then
        coins = math_ramdon(coin_conf.min,coin_conf.max)
    end
    local ok,base_data = R().basesvr({key=uid}):call('.msg_handler','get_base_data',uid)
    if not ok then
        errlog(uid,'failed to add coins for robot',coins)
        return false
    end
    
    dbglog(uid,'base_data.coins base_data.coins base_data.coins',base_data.coins) 
    if base_data.coins >= coins then
        robot_info.coins = base_data.coins
        dbglog(uid,'this robot does not need more coins',base_data.coins,coins)        
        return true
    end

    local ok,succ,ret = R().basesvr{key=uid}:call('.msg_handler',
        'add_coins',uid,coins,reason.ADD_COINS_FOR_ROBOT, {is_robot = true})
    if not ok then
        errlog(uid,'failed to add coins for robot',coins)
        return false
    end

    if not succ then
        errlog(uid,'failed to add coins for robot,not successful',coins)
        return false
    end

    robot_info.coins = ret.curr
    
    dbglog(uid,'this robot needs more coins',base_data.coins,coins,ret.curr)

    return true
end

debug.setupvalue(lua.REQUESTS.get_robot,idx,full_coins_for_robot)